//
//  CZCommentCell.swift
//  GZWeibo007
//
//  Created by 易辉华 on 15/12/25.
//  Copyright © 2015年 Apple. All rights reserved.
//

import UIKit

class CZCommentCell: UITableViewCell {

    //MARK: - 微博模型
    var model: CZDetailCommentModel? {
        didSet {
            //头像
            if let iconUrl = model?.user?.profile_image_url {
                iconView.sd_setImageWithURL(NSURL(string: iconUrl))
            }
            //时间
            timeLabel.text = model?.created_at ?? "未知时间"
            //名称
            nameLabel.text = model?.user?.screen_name
            //认证类型
            verifiedView.image = model?.user?.verifiedImage
            //会员等级
            memberView.image = model?.user?.mbrankImage
            //内容
            contentLabel.text = model?.text
        }
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        prepareUI()
    }

    //准备UI
    func prepareUI() {
        //添加控件
        contentView.addSubview(iconView)
        contentView.addSubview(nameLabel)
        contentView.addSubview(timeLabel)
        contentView.addSubview(verifiedView)
        contentView.addSubview(memberView)
        contentView.addSubview(contentLabel)
        
//        //取消约束
//        iconView.translatesAutoresizingMaskIntoConstraints = false
//        nameLabel.translatesAutoresizingMaskIntoConstraints = false
//        timeLabel.translatesAutoresizingMaskIntoConstraints = false
//        verifiedView.translatesAutoresizingMaskIntoConstraints = false
//        memberView.translatesAutoresizingMaskIntoConstraints = false
//        contentLabel.translatesAutoresizingMaskIntoConstraints = false
    
        //设置约束
        /// 头像视图
        iconView.ff_AlignInner(type: ff_AlignType.TopLeft, referView: contentView, size: CGSize(width: 35, height: 35), offset: CGPoint(x: 8, y: 8))
        
        /// 名称
        nameLabel.ff_AlignHorizontal(type: ff_AlignType.TopRight, referView: iconView, size: nil, offset: CGPoint(x: 8, y: 0))
        
        /// 时间
        timeLabel.ff_AlignHorizontal(type: ff_AlignType.BottomRight, referView: iconView, size: nil, offset: CGPoint(x: 8, y: 0))
        
        /// 会员等级
        memberView.ff_AlignHorizontal(type: ff_AlignType.CenterRight, referView: nameLabel, size: CGSize(width: 14, height: 14), offset: CGPoint(x: 8, y: 0))
        
        /// 认证图标
        verifiedView.ff_AlignInner(type: ff_AlignType.BottomRight, referView: iconView, size: CGSize(width: 17, height: 17), offset: CGPoint(x: 8.5, y: 8.5))
        
        /// 内容
        contentLabel.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: timeLabel, size: nil, offset: CGPoint(x: 0, y: 8))
        contentView.addConstraint(NSLayoutConstraint(item: contentLabel, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: -8))
        
        // 底部约束
        contentView.addConstraint(NSLayoutConstraint(item: contentView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: contentLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 8))
        
        
    }
    
    
    
    
    //MARK: - 懒加载
    ///用户头像
    lazy var iconView = UIImageView()
    ///用户名称
    lazy var nameLabel: UILabel = {
        let nameLabel = UILabel()
        nameLabel.textColor = UIColor.darkGrayColor()
        nameLabel.font = UIFont.systemFontOfSize(14)
        return nameLabel
    }()
    ///时间label
    lazy var timeLabel: UILabel = {
        let nameLabel = UILabel()
        nameLabel.textColor = UIColor.orangeColor()
        nameLabel.font = UIFont.systemFontOfSize(9)
        return nameLabel
    }()
    ///认证图标
    lazy var verifiedView = UIImageView()
    ///会员等级
    lazy var memberView: UIImageView = {
        let memberView = UIImageView()
        memberView.image = UIImage(named: "common_icon_membership")
        return memberView
    }()
    ///内容
    lazy var contentLabel: UILabel = {
        let contentLabel = UILabel()
        contentLabel.textColor = UIColor.darkGrayColor()
        contentLabel.font = UIFont.systemFontOfSize(16)
        contentLabel.numberOfLines = 0
        return contentLabel
    }()
    
    
}
